package com.gxpt.app.view;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.gxpt.app.bean.normal;
import com.gxpt.app.bean.showcontact_bean;
import com.utils.T;
import com.utils.logger.Logger;
import com.utils.okhttputils.HttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import okhttp3.Call;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_webview_showcontact)
public class Activity_webview_showcontact extends BaseActivity {

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.mWebView)
    WebView mWebView;


    @ViewInject(R.id.toCancel)
    Button toCancel;

    @ViewInject(R.id.toSubmit)
    Button toSubmit;

    String url="";//url
    Bundle d;
    String ID = "";
    String contact = "";

    int type = 1;//1甲方2乙方
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        d  = getIntent().getBundleExtra("DATA");
        if(d!=null){
            ID = d.getString("ID");
            type = d.getInt("TYPE");
        }
        initView();
        initData();
    }

    @Override
    public void initView() {

        txt_title.setText(getResources().getString(R.string.txt_wangqianhetong));


        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        mWebView.requestFocus();
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.setHorizontalScrollBarEnabled(false);//水平不显示
        mWebView.setVerticalScrollBarEnabled(false); //垂直不显示
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        mWebView.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon){
                hud.show();

            }
            public void onPageFinished(WebView view, String url){
                try{
                    if(hud.isShowing()){
                        hud.dismiss();
                    }
                }catch(Exception e){
                    e.printStackTrace();
                    hud.dismiss();
                }


            }
        });

//        url = Constant.URL_H5_goods_detail+ID;
//        mWebView.loadUrl(url);
    }

    @Override
    public void initData() {
        toNet_showcontact();
    }



    @Event(value={R.id.toback,R.id.toCancel,R.id.toSubmit})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
            case R.id.toCancel:
                closeActivity();
                break;
            case R.id.toSubmit:
                do_signcontact();
                break;
        }
    }


    //////NET DO
    private void toNet_showcontact(){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "demand");
            jsonBody.put("serviceName",          "showcontact");

            jsonBody.put("member_id",             AppUtils.get_user_id(context));
            jsonBody.put("demand_id",             ID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i("toNet_servicelist = "+jsonBody.toString());
        HttpUtils.http_post_string(Constant.URL_showcontact,jsonBody.toString(),do_showcontact);
    }

    StringCallback do_showcontact = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e("do_servicelist = "+s);
            try{
                showcontact_bean data = new Gson().fromJson(s, showcontact_bean.class);
                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    if(data.data != null){
                        contact = data.data;
                          mWebView.loadData(data.data+"<br/><br/><br/><br/><br/><br/>", "text/html; charset=UTF-8", null);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };


    // //////NET DO
    private void do_signcontact(){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "demand");
            jsonBody.put("serviceName",          "signcontact");

            jsonBody.put("type",                 type);
            jsonBody.put("member_id",             AppUtils.get_user_id(context));
            jsonBody.put("demand_id",             ID);
            jsonBody.put("contact",              contact);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i("do_signcontact = "+jsonBody.toString());
        HttpUtils.http_post_string(Constant.URL_signcontact,jsonBody.toString(),do_signcontact);
    }

    StringCallback do_signcontact = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e("do_signcontact = "+s);
            try{
                normal data = new Gson().fromJson(s, normal.class);
                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    EventBus.getDefault().post(new MessageEvent(Constant.ACTION_hetong_siged));
                    T.show(context,""+getResources().getString(R.string.alert_success_do),0);
                    closeActivity();
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };
}



